package com.global.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.global.dao.QueryParameterDefinitionDao;
import com.global.model.DataSourceX;
import com.global.model.QueryParameterDefinition;

@Repository("queryParameterDefinitionDao")
public class QueryParameterDefinitionDaoImpl extends GenericDaoHibernate<QueryParameterDefinition, Long> implements QueryParameterDefinitionDao {

	public QueryParameterDefinitionDaoImpl() {
		super(QueryParameterDefinition.class);
	}

	/**
	 * {@inheritDoc}
	 */
	public QueryParameterDefinition saveQueryParameterDefinition(QueryParameterDefinition queryParameterDf) {
		if(queryParameterDf!=null){
			if(log.isDebugEnabled()){
				log.debug("queryParameterDefinition id :" + queryParameterDf.getId());
			}
			getHibernateTemplate().saveOrUpdate(queryParameterDf);
			getHibernateTemplate().flush();
		}

		return queryParameterDf;
	}

	/**
	 * {@inheritDoc}
	 */
	public void saveBatchQueryParameterDefinitions(List<QueryParameterDefinition> queryParameterDfList) {
		this.saveBatchQueryParameterDefinitions(queryParameterDfList, null);
		
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void saveBatchQueryParameterDefinitions(List<QueryParameterDefinition> saveList, List<QueryParameterDefinition> deleteList) {
		if(deleteList!=null){
			getHibernateTemplate().deleteAll(deleteList);
		}
		if(saveList!=null){
			getHibernateTemplate().saveOrUpdateAll(saveList);
		}
		
		getHibernateTemplate().flush();
	}

	/**
	 * {@inheritDoc}
	 */
	public QueryParameterDefinition getQueryParameterDefinition(DataSourceX dataSourceX, String parameterName) {
		if(dataSourceX!=null){
			return this.getQueryParameterDefinition(dataSourceX.getId(), parameterName);
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public QueryParameterDefinition getQueryParameterDefinition(Long dataSourceXId, String parameterName) {
		if(dataSourceXId!=null && StringUtils.isNotBlank(parameterName)){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("dataSourceXId", dataSourceXId);
	        queryParams.put("parameterName", parameterName);
			List<QueryParameterDefinition> list = findByNamedQuery("findQueryParameterDefinitionByDataSourceXAndParameterName", queryParams);
			if(!list.isEmpty()){
				return list.get(0);
			}
		}

		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public List<QueryParameterDefinition> getQueryParameterDefinitionList(DataSourceX dataSourceX) {
		if(dataSourceX!=null){
			return getQueryParameterDefinitionList(dataSourceX.getId());
		}
		
		return new ArrayList<QueryParameterDefinition>();
	}

	/**
	 * {@inheritDoc}
	 */
	public List<QueryParameterDefinition> getQueryParameterDefinitionList(Long dataSourceXId) {
		if(dataSourceXId!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("dataSourceXId", dataSourceXId);
			
			return findByNamedQuery("findQueryParameterDefinitionByDataSourceX", queryParams);
		}
		
		return new ArrayList<QueryParameterDefinition>();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void deleteQueryParameterDefinition(QueryParameterDefinition QueryParameterDf) {
		getHibernateTemplate().delete(QueryParameterDf);
		getHibernateTemplate().flush();
	}

	/**
	 * {@inheritDoc}
	 */
	public void deleteBatchQueryParameterDefinitions(List<QueryParameterDefinition> queryParameterDefinitionList) {
		this.saveBatchQueryParameterDefinitions(null, queryParameterDefinitionList);
	}

	/**
	 * {@inheritDoc}
	 */
	public List<QueryParameterDefinition> getFixedQueryParameterDefinitionList(DataSourceX dataSourceX) {
		if(dataSourceX!=null){
			return getFixedQueryParameterDefinitionList(dataSourceX.getId());
		}
		
		return new ArrayList<QueryParameterDefinition>();
	}

	/**
	 * {@inheritDoc}
	 */
	public List<QueryParameterDefinition> getFixedQueryParameterDefinitionList(Long dataSourceXId) {
		if(dataSourceXId!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("dataSourceXId", dataSourceXId);
			queryParams.put("isFilter", false);
			
			return findByNamedQuery("findQueryParameterDefinitionsByDataSourceXAndFilter", queryParams);
		}
		
		return new ArrayList<QueryParameterDefinition>();
	}

	/**
	 * {@inheritDoc}
	 */
	public List<QueryParameterDefinition> getFilterQueryParameterDefinitionList(DataSourceX dataSourceX) {
		if(dataSourceX!=null){
			return getFilterQueryParameterDefinitionList(dataSourceX.getId());
		}
		
		return new ArrayList<QueryParameterDefinition>();
	}

	/**
	 * {@inheritDoc}
	 */
	public List<QueryParameterDefinition> getFilterQueryParameterDefinitionList(Long dataSourceXId) {
		if(dataSourceXId!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("dataSourceXId", dataSourceXId);
			queryParams.put("isFilter", true);
			
			return findByNamedQuery("findQueryParameterDefinitionsByDataSourceXAndFilter", queryParams);
		}
		
		return new ArrayList<QueryParameterDefinition>();
	}
}
